﻿using xtraSMSProviderHandler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xtraSMSCore
{
    public class Core
    {
        //Hilfs Variabeln

        //Provider -> Schnittstelle für veraltete Programme
        public IProvider wsmsh;

        //Files
        public string ConfigFile { get; set; }
        public string PluginFolder { get; set; }
        public bool IsLoggedIn { get; private set; }

        //UserSettings
        public Boolean SaveCredentials { get; set; }
        public Boolean UseOutlook { get; set; }

        public IProvider[] ProviderList
        {
            get
            {
                return LoadProviders(PluginFolder, "IProvider");
            }
        }

        //Konstruktor
        public Core()
        {

        }

        //Loggt sich in das System ein
        public bool Login(string Username, string Password)
        {
            wsmsh.LoginName = Username;
            wsmsh.Password = Password;

            if (wsmsh.Login())
            {
                IsLoggedIn = true;
                return true;
            }
            else
            {
                IsLoggedIn = false;
                return false;
            }
        }

        //Loggt sich erneut mit den selben userdaten an
        public bool ReLogin()
        {
            if (wsmsh.Login())
            {
                IsLoggedIn = true;
                return true;
            }
            else
            {
                IsLoggedIn = false;
                return false;
            }
        }

        //Sendet eine SMS
        public bool SendSMS(string recipients, string message)
        {
            Boolean result = true;
            recipients = recipients.Replace(" ", string.Empty);
            recipients = recipients.Replace("+41", "0041");
            string[] contacts = recipients.Split(new Char[] { ',', ';' });
            foreach (string recipient in contacts)
            {
                if(!string.IsNullOrEmpty(recipient))
                {
                    if(!wsmsh.SendSMS(recipient, message))
                    {
                        result = false;
                    }
                }
            }
            return result;
        }

        //Listet alle Provider auf
        private IProvider[] LoadProviders(string directory, string strInterface)
        {
            IProvider[] wsmshProvider;
            PluginServices.AvailablePlugin[] providers = PluginServices.FindPlugins(directory, strInterface);
            
            if (providers != null)
            {
                Int32 counter = 0;
                wsmshProvider = new IProvider[providers.Length];
                foreach (PluginServices.AvailablePlugin provider in providers)
                {
                    wsmshProvider[counter] = (IProvider)PluginServices.CreateInstance(provider);
                    counter++;
                }
            }
            else
            {
                wsmshProvider = new IProvider[0];
            }

            return wsmshProvider;
        }

        //Setzt den Provider
        public void SetProvider(IProvider Provider)
        {
            wsmsh = Provider;
            wsmsh.Initialize();
        }

        //Gibt die Account Infos zurueck
        public AccountInformation GetInformation() { return wsmsh.GetInformation(); }
    }
}
